const express = require('express');
const mysql = require('mysql');
const os = require('os');

const networkInterfaces = os.networkInterfaces();
for (const interfaceName in networkInterfaces) {
    // 遍历每个网络接口
    networkInterfaces[interfaceName].forEach((interface) => {
        if (interface.family === 'IPv4' && !interface.internal) {
            // 仅打印IPv4地址且排除内部接口
            console.log(`Interface: ${interfaceName}, IP Address: ${interface.address}`);
        }
    });
}

const app = express()

app.use(express.json())

app.all('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Content-Type')

    if (res.methods === 'OPTIONS') {
        res.sendStatus(200)
    } else {
        next()
    }

})

// -- Active: 1716623599794@@127.0.0.1@3306
// CREATE DATABASE 


// -- 创建数据库
// CREATE DATABASE blog;
//     DEFAULT CHARACTER SET = 'utf8mb4';

// -- 使用数据库
// USE blog;

// -- 创建用户信息表
// CREATE TABLE users (
//     id INT AUTO_INCREMENT PRIMARY KEY,
//     username VARCHAR(255) NOT NULL,
//     password VARCHAR(255) NOT NULL,
//     name VARCHAR(255) NOT NULL,
//     contact VARCHAR(255),
//     purpose TEXT,
//     role VARCHAR(255) NOT NULL
// );

// --查询用户权限
// GRANT ALL PRIVILEGES ON yourdatabase.* TO 'root'@'localhost' IDENTIFIED BY 'hdjz30536';
// FLUSH PRIVILEGES;




// 创建 MySQL 连接池
const db = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: 'hdjs30536',
    database: 'blog'
});

// 测试数据库连接
db.getConnection((err, connection) => {
    if (err) throw err;
    console.log('数据库连接成功');
    connection.release();
});



app.post('/login', (req, res) => {
    const { username, password, name, contact, role } = req.body;

    const sql1 = ` select * from users where username = + ${username} `
    db.query(sql1, (err, rows) => {
        if (err) {
            console.error('数据查找错误：', err);
            res.status(500).send('DB error');
        }
        else if (rows.length == 1) {
            const sql2 = `select * from users where username = + ${username} +  and password =  + ${password}`
            db.query(sql2, (err, rows) => {
                if (err) {
                    console.error('数据验证错误：', err);
                    res.status(500).send('DB error');
                }
                if (rows.lenght > 0) {
                    res.status(200).send('登录成功')
                }
            })
        }
        else if (rows.length == 0) {
            const sql = 'INSERT INTO users (username, password, name, contact, purpose, role) VALUES (?, ?, ?, ?, ?, ?)';
            const values = [username, password, name, contact, JSON.stringify(purpose), role];
            db.query(sql, values, (err, results) => {
                if (err) {
                    console.error('数据插入错误', err);
                    res.status(500).send('Server error');
                } else {
                    res.status(200).send('User data saved successfully');
                }
            });

        }
        else {
            res.status(400).send('账号出错')
        }
    })
})



app.listen(5362, () => {
    console.log('server listening on 5362')
})


